jQuery(function($){
	//서브페이지 lnb
	$('.leftMenu > .depth1 > li').find('.depth2').hide();
	$('.leftMenu > .depth1 > li > a').bind('click', function(){
		var fold = $(this).next().children().attr('src').replace('icon_lnbmore', 'icon_lnbminus');
		var more = $(this).next().children().attr('src').replace('icon_lnbminus', 'icon_lnbmore');
		$('.leftMenu > .depth1 > li').find('.depth2').slideUp('fast');
		$(this).parent().find('.depth2').stop().slideDown('fast');
		if ($(this).next().next().css('display') == 'block') {
			$('.leftMenu > .depth1 > li > a').next().removeClass('fold').addClass('open').children().attr('src', more);
			$(this).next().removeClass('open').addClass('fold').children().attr('src', fold);
		}
		if ($(this).closest('.depth1').find('.depth2') != 0) {
			return false;
		}
	});

	$('.leftMenu > .depth1 > li').bind('mouseenter focus', function(){
		$(this).addClass('on');
	});
	$('.leftMenu > .depth1 > li').bind('mouseleave blur', function(){
		$(this).removeClass('on');
	});

	$('.leftMenu .depth2 > li > a').bind('click', function(){
		if ($(this).parent().parent().css('display') == 'block') {
			$('.leftMenu .depth2 > li > a').parent().removeClass('current');
			$(this).parent().addClass('current');
		}
	});
	lnbInit();

	if ($('.leftMenu .depth2 > li > a').parent().hasClass('current')) {
		$('.leftMenu .depth2 > li.current').parent().show();
	}
	
});

var lnbInit = function(){
	if (window.location.href.indexOf('lnbNum') != -1) {
		var linkNum = window.location.href.split('?')[1]; //?lnbNum='lnb index(숫자)'
		linkNum = linkNum.split('=')[1];
		$('.leftMenu .depth2 > li').removeClass('current');
		$('.leftMenu .depth2 > li').eq(linkNum-1).addClass('current');
	}
};

jQuery(function($){
	//공통 탭
	var $tabLink = $('.tabWrap > ul > li > a');
	var $target = $('.tabContent > div');
	if ($tabLink.parent().length == $target.length) {
		$target.hide();
		$target.each(function(idx){
			var num = idx+1;
			$(this).attr('id', 'tabCon'+num);
		});
		$tabLink.each(function(idx){
			var num = idx+1;
			$(this).attr('href', '#tabCon'+num);
		});
		$tabLink.bind('click', function(){
			$target.hide();
			if ($(this).parent().addClass('on')) {
				$tabLink.parent().removeClass('on');
				var $href = $(this).attr('href');
				$($href).show();
				$(this).parent().addClass('on');
			}
			return false;
		});
		$tabLink.eq(0).click();
	}
});

//레이어 팝업
$(window).load(function(){
	var $mask = $('<div class="mask"></div>'); //불투명 배경div생성
	var $hrefObj; //호출하는 a링크 href
	var $layer; // 레이어 팝업 id값
	var $layerWidth; // 레이어 팝업 (마진,패딩 모두 포함)한 넓이
	var $layerHeight; // 레이어 팝업 (마진,패딩 모두 포함)한 높이

	if ($('.layer_call').length && $('.layer_popup').length != 0) {
		$('.layer_popup').hide();
		$('.layer_call').bind('click', function(e){
			e.preventDefault();
			$('.current').removeClass('current'); //접근성
			$(this).addClass('current');//접근성
			$mask.appendTo('body');
			$mask.fadeIn().css({'width':'100%', 'height':$(document).outerHeight()});

			$hrefObj = $(this).attr('href');
			$layer = $($hrefObj);
			$layerWidth = $layer.outerWidth();
			$layerHeight = $layer.outerHeight();

			$layer.fadeIn().css({'top':($(window).height()/2) - $layerHeight/2 + $(window).scrollTop()/*, 'left':($(window).width()-$layerWidth)/2*/}); //fadeIn효과
			//$layer.show().stop().animate({'top':($(window).height()-$layerHeight)/2, 'left':($(window).width()-$layerWidth)/2}, 400); //슬라이드 효과
			$layer.offset({left:($(window).width()-$layerWidth)/2});
			$('.layer_popup').attr('tabindex', '0').focus();
			
			if ($mask.length != 0) {
				$('.mask, .layer_close').bind('click', function(e){
					e.preventDefault();
					$('.mask').fadeOut();
					$layer.fadeOut(); //fadeIn효과
					/*
					$layer.stop().animate({'left':'9999px'}, 400, function(){
						$(this).css('left', '-9999px').hide();
					}); //슬라이드 효과*/
					//$('html, body').css({'height':'auto', 'overflow':'visible'});
					$('.current').focus();//접근성
				});
			}
			//$('html, body').css({'height':$(window).height(), 'overflow':'hidden'});
		});
		$(window).resize(function(){
			//$('html, body').css('height', $(window).height());
			$mask.css({'width':$(document).width(), 'height':$(document).height()});
			$('.layer_popup').css({'top':($(window).height()/2) - $layerHeight/2 + $(window).scrollTop()/*, 'left':($(window).width()-$layerWidth)/2*/});
			$layer.offset({left:($(window).width()-$layerWidth)/2});
		});
	}
});